import 'package:flutter/material.dart';

import 'package:date_format/date_format.dart' as DateFormat;
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class DatePickerPubDemo extends StatefulWidget {
  DatePickerPubDemo({Key key}) : super(key: key);

  @override
  _DatePickerPubDemoState createState() => _DatePickerPubDemoState();
}

class _DatePickerPubDemoState extends State<DatePickerPubDemo> {

  DateTime _dateTime = DateTime.now();
  _showDatePicker() {
    DatePicker.showDatePicker(
      context,
      onMonthChangeStartWithFirstDate: true,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确定', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.cyan)),
      ),
      minDateTime: DateTime.parse('1990-01-01'),
      maxDateTime: DateTime.parse('2100-01-01'),
      initialDateTime: DateTime.now(),
      
      // dateFormat: 'yyyy-MMMM-dd',
      // 年月日时分
      dateFormat: 'yyyy年M月d日    EEE,H时:m分',
      pickerMode: DateTimePickerMode.datetime, // show TimePicker
      
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      // onChange: (dateTime, List<int> index) {
      //   setState(() {
      //     _dateTime = dateTime;
      //   });
      // },
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('日期选择') 
      ), 
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          InkWell(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center, 
              children: <Widget>[
                // Text("${DateFormat.formatDate(_dateTime, ['yyyy', '年', 'mm', '月', 'dd', '日'])}"),
                // Text("${DateFormat.formatDate(_dateTime, ['yyyy', '年', 'mm', '月', 'dd', '日', '  ', 'HH','时', 'nn', '分'])}"),
                Text("${DateFormat.formatDate(_dateTime, ['yyyy','-','mm','-','dd', '  ', 'HH',':','nn'])}"),
                Icon(Icons.arrow_drop_down)
              ],
            ),
            onTap: _showDatePicker,
          )
        ], 
      ),
    );
  }
}